package com.xijia.wuyouwallet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.bean.UpGradeData;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2017/9/11 0011.
 */

public class UpgradeAdapter extends BaseAdapter {

    private List<UpGradeData.DataBean.ListBean> list;
    private Context context;
    private OnUpClick onUpClick;
    public UpgradeAdapter(List<UpGradeData.DataBean.ListBean> list,Context context,OnUpClick click){
        this.list = list;
        this.context = context;
        this.onUpClick = click;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view==null){
            view = LayoutInflater.from(context).inflate(R.layout.list_item_upgrade,null);
            holder = new ViewHolder();
            holder.item_upgrade_img = (ImageView) view.findViewById(R.id.item_upgrade_img);
            holder.item_upgrade_name = (TextView) view.findViewById(R.id.item_upgrade_name);
            holder.item_upgrade_rate = (TextView) view.findViewById(R.id.item_upgrade_rate);
            holder.item_upgrade_content1 = (TextView) view.findViewById(R.id.item_upgrade_content1);
            holder.item_upgrade_content2 = (TextView) view.findViewById(R.id.item_upgrade_content2);
            holder.item_upgrade_up_img = (ImageView) view.findViewById(R.id.item_upgrade_up_img);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        Picasso.with(context).load(list.get(i).getMember_head_img()).fit().into(holder.item_upgrade_img);
        holder.item_upgrade_name.setText(list.get(i).getMember_name());
        double rate = Double.parseDouble(list.get(i).getMember_rate())/10;
        DecimalFormat dformat = new DecimalFormat("#0.00");
        String rateFormat = dformat.format(rate);
        //double charge = Double.parseDouble(list.get(i).getMember_service_charge());
        //String chargeFormat = dformat.format(charge);
        holder.item_upgrade_rate.setText(rateFormat+"%"+" + "+list.get(i).getMember_service_charge());
        double money = Double.parseDouble(list.get(i).getMember_up_money());
        String moneyFormat = dformat.format(money);
        if (list.get(i).getMember_id().equals("4")){
            holder.item_upgrade_content1.setText("支付"+moneyFormat+"元");
        }else {
            holder.item_upgrade_content1.setText("邀请"+list.get(i).getMember_recommend_num()+"人开通,或支付"+moneyFormat+"元");
        }
        holder.item_upgrade_content2.setText(list.get(i).getMember_up_describe());
        if (list.get(i).getIsUp()==0){
            holder.item_upgrade_up_img.setVisibility(View.GONE);
        }else if (list.get(i).getIsUp()==1){
            holder.item_upgrade_up_img.setVisibility(View.VISIBLE);
        }
        holder.item_upgrade_up_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onUpClick.click(i);
            }
        });
        return view;
    }

    private class ViewHolder{
        ImageView item_upgrade_img,item_upgrade_up_img;
        TextView item_upgrade_name,item_upgrade_rate,item_upgrade_content1,item_upgrade_content2;
    }

    public interface OnUpClick{
        void click(int postion);
    }

}
